/**
 * Mode.c
 * Description: The operating mode of the system
 * Authors: Hamish Cumming, Brian Thorne 2008
 */
#include "mode.h"
#include "ui.h"
#include "kernel.h"
#include "measure.h"

/* Private Variables */
static mode_t mode = IDLE; //current operating mode


/** Changes the operating mode */
void set_mode(mode_t new_mode)
{
    mode = new_mode;
    switch (mode)
    {
        case IDLE:
            ui_message("idling");
            disable_measure();
            deactivate_task(REPLAY_TASK);
            break;
        case MEASURE:
            ui_message("measuring");
            reset_stats();
            deactivate_task(REPLAY_TASK);
            //turn on the pin change interrupts and start measuring
            enable_measure();
            break;
        case REPLAY:
            disable_measure(); //disable measuring; dont collect any more stats
            activate_task(REPLAY_TASK);
            break;
    }
}

/** Called to get the mode */
mode_t get_mode()
{
    return mode;
}

/** Provides an interface for different modes to use joystick actions */
void mode_action(direction_t dir)
{
    if (mode == REPLAY)
    {
        if (dir == up)
        {
            scroll_stats(SCROLL_UP);
        }
        else if (dir == down)
        {
            scroll_stats(SCROLL_DOWN);
        }
    }

}

/** Entrypoint */
int main (void)
{
    /* Initialise the kernel */
    set_mode(IDLE);
    kernel_init();
    /* Set up modules */
    ui_init();
    measure_init();
    register_task(replay_task, REPLAY_TASK);
    /* Give Control to the Kernel */
    return (int) kernel_run();
}
